package com.ct.gd.passport.view.ticket;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.ct.gd.passport.R;
import com.ct.gd.passport.domain.TicketInfo;
import com.ct.gd.passport.view.BundleKeys;
import com.up91.common.android.content.SimpleLoader;
import com.up91.common.android.view.adapter.SimpleListAdapter;

import java.util.List;

public class TicketInfoFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<TicketInfo>>, AdapterView.OnItemClickListener {

    private ListView mLVInfo;
    private TicketInfoFragment.TicketInfoAdapter mTicketInfoAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.ticket_info_list, null);
        mLVInfo = (ListView) v.findViewById(android.R.id.list);
        mLVInfo.setOnItemClickListener(this);
        mTicketInfoAdapter = new TicketInfoAdapter(getActivity(), null);
        mLVInfo.setAdapter(mTicketInfoAdapter);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        int type = getActivity().getIntent().getIntExtra(BundleKeys.TICKET_TYPE, 1);
        Bundle args = new Bundle();
        args.putInt(BundleKeys.TICKET_TYPE, type);
        getLoaderManager().initLoader(0, args, this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.putExtra(BundleKeys.TICKET_INFO, ((TicketInfo) parent.getAdapter().getItem(position)));
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
    }

    private static class TicketInfoLoader extends SimpleLoader<List<TicketInfo>> {
        public int type;

        public TicketInfoLoader(Context context, View view) {
            super(context, view);
        }

        @Override
        protected List<TicketInfo> loadData() throws Exception {
            return TicketInfo.load(type);
        }
    }

    @Override
    public Loader<List<TicketInfo>> onCreateLoader(int id, Bundle args) {
        int type = args.getInt(BundleKeys.TICKET_TYPE);
        TicketInfoLoader loader = new TicketInfoLoader(getActivity(), mLVInfo);
        loader.type = type;
        return loader;
    }

    @Override
    public void onLoadFinished(Loader<List<TicketInfo>> loader, List<TicketInfo> data) {
        if (null != data) {
            mTicketInfoAdapter.setData(data);
            mTicketInfoAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onLoaderReset(Loader<List<TicketInfo>> loader) {
    }

    private static class TicketInfoAdapter extends SimpleListAdapter<TicketInfo> {
        private static class ViewHolder {
            TextView tvSerial;
            TextView tvTerminal;
            TextView tvComment;
            TableLayout table;
        }

        public TicketInfoAdapter(Context context, List<TicketInfo> data) {
            super(context, data);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (null == convertView) {
                convertView = mInflater.inflate(R.layout.ticket_info_list_item, null);
                holder = new ViewHolder();
                holder.tvSerial = (TextView) convertView.findViewById(R.id.ticket_info_serial_tv);
                holder.tvTerminal = (TextView) convertView.findViewById(R.id.ticket_info_terminal_tv);
                holder.tvComment = (TextView) convertView.findViewById(R.id.ticket_info_comment_tv);
                holder.table = (TableLayout) convertView.findViewById(R.id.ticket_info_start_info_area);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            TicketInfo t = getData().get(position);

            TextView tvSerial = holder.tvSerial;
            TextView tvTerminal = holder.tvTerminal;
            TextView tvComment = holder.tvComment;
            TableLayout table = holder.table;

            setContent(t, tvSerial, tvTerminal, tvComment, table);
            return convertView;
        }
    }

    static void setContent(TicketInfo t, TextView tvSerial, TextView tvTerminal, TextView tvComment, TableLayout table) {
        tvSerial.setText(t.serial);
        tvTerminal.setText(t.terminal);
        tvComment.setText(t.comment);

        table.removeViews(1, table.getChildCount() - 1);
        Context context = table.getContext();
        for (TicketInfo.StartInfo info : t.start) {
            TableRow row = new TableRow(context);
            TextView t1 = new TextView(context);
            TextView t2 = new TextView(context);
            t1.setText(info.time);
            t2.setText(info.place);
            row.addView(t1);
            row.addView(t2);
            table.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
        }
    }

}
